package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.entity.SelectMediaEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.PriVideoAdapter;
import flc.ast.adapter.PriVideoAdapter1;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.adapter.SelVideoAdapter1;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.ActivityPrivacyVideoBinding;
import flc.ast.dialog.EditDialog;
import flc.ast.dialog.InfoDialog;
import flc.ast.dialog.PriEditDialog;
import flc.ast.dialog.PriStyleDialog;
import flc.ast.dialog.RenameDialog;
import flc.ast.dialog.StyleDialog;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.SPUtil;

public class PrivacyVideoActivity extends BaseAc<ActivityPrivacyVideoBinding> {

    private List<MyVideoBean> listShow = new ArrayList<>();
    private PriVideoAdapter videoAdapter;
    private PriVideoAdapter1 videoAdapter1;
    private boolean hasLineStyle = true;
    private PriStyleDialog mStyleDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_privacy_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPrivacyVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivLayout.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new LinearLayoutManager(mContext));
        videoAdapter = new PriVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.addChildClickViewIds(R.id.ivSelVideoItemEdit);
        videoAdapter.setOnItemChildClickListener(this);
        videoAdapter.setOnItemClickListener(this);

        videoAdapter1 = new PriVideoAdapter1();
        videoAdapter1.addChildClickViewIds(R.id.ivSelVideoItemEdit);
        videoAdapter1.setOnItemChildClickListener(this);
        videoAdapter1.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivLayout:
                showRankDialog();
                break;
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivSelVideoItemEdit://编辑
                showEditDialog(position);
                break;
        }
    }

    @Override
    protected void initData() {
        //是否开启隐蔽模式
        boolean isHideMode = SPUtil.getBoolean(mContext, Content.isHideMode, false);
        if (isHideMode) {
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            mDataBinding.tvVideoNoData.setVisibility(View.VISIBLE);
        } else {
            getData();
        }
    }

    private void getData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myPrivacyVideo;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(new MyVideoBean(file.getPath(), file.getName(), MediaUtil.getDuration(file.getPath()), file.length()));
            }
        }
        if (listShow.size() > 0) {
            videoAdapter.setList(listShow);
            mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
            mDataBinding.tvVideoNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvVideoList.setVisibility(View.GONE);
            mDataBinding.tvVideoNoData.setVisibility(View.VISIBLE);
        }
    }

    public void showRankDialog() {
        if (mStyleDialog == null) {
            mStyleDialog = new PriStyleDialog(mContext);
        }
        mStyleDialog.setListener(new PriStyleDialog.Ilistener() {
            @Override
            public void clickListLayout() {
                hasLineStyle = true;
                mDataBinding.rvVideoList.setLayoutManager(new LinearLayoutManager(mContext));
                mDataBinding.rvVideoList.setAdapter(videoAdapter);

            }

            @Override
            public void clickGridLayout() {
                hasLineStyle = false;
                mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 2));
                mDataBinding.rvVideoList.setAdapter(videoAdapter1);

            }
        });
        mStyleDialog.show();
    }


    private void showEditDialog(int position) {
        PriEditDialog dialog = new PriEditDialog(mContext);
        dialog.bean = listShow.get(position);
        dialog.setListener(new PriEditDialog.Ilistener() {
            @Override
            public void clickRename() {
                showRenameDialog(position);
            }

            @Override
            public void clickShare() {
                IntentUtil.shareVideo(mContext, listShow.get(position).getPath());
            }

            @Override
            public void clickAddPrivacyFolder() {
                delete(position);
            }

            @Override
            public void clickInfo() {
                showInfoDialog(position);
            }

            @Override
            public void clickDelete() {
                delete(position);
            }
        });
        dialog.show();
    }

    private void showRenameDialog(int position) {
        RenameDialog dialog = new RenameDialog(mContext);
        dialog.title = getString(R.string.file_rename);
        dialog.hint = getString(R.string.please_input_folder_name_text);
        dialog.setListener(new RenameDialog.Ilistener() {
            @Override
            public void clickRename(String name) {
                FileUtils.rename(listShow.get(position).getPath(), name + "." + FileUtils.getFileExtension(listShow.get(position).getPath()));
                getData();
            }
        });
        dialog.show();
    }

    private void delete(int position) {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        FileUtils.delete(listShow.get(position).getPath());
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        getData();
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                    }
                });
            }
        }, 1000);
    }

    private void showInfoDialog(int position) {
        InfoDialog dialog = new InfoDialog(mContext);
        dialog.videoPath = listShow.get(position).getPath();
        dialog.show();
    }


}